//
//  CustomerListViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/3.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "CustomerListViewController.h"
#import "HttpCustomerAction.h"
#import "CustomerDetailViewController.h"
#import "CustomerInfo.h"
#import "AddCustomerViewController.h"

@interface CustomerListViewController ()

@end

@implementation CustomerListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"投资客户列表";
    self.tableView.separatorStyle = 0;
//    CustomerActionInfo *customerActionInfo = [[CustomerActionInfo alloc] init];
//    [HttpCustomerAction getCustomerList:customerActionInfo block:^(id result, NSError *error) {
//        if (!error) {
//
//        }
//    }];
    UIButton *rightbutton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
    [rightbutton setTitle:@"+" forState:UIControlStateNormal];
    [rightbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(addCustomerAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    __weak CustomerListViewController *filterView = self;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [filterView loadHeader];
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = header;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadFooter)];
    
    CustomerActionInfo *customerActionInfo = [[CustomerActionInfo alloc] init];
    [self setClass:@"HttpCustomerAction" action:@selector(getCustomerList:block:) object:customerActionInfo];
    
    [HttpCustomerAction getAllCustomerGain:^(id result, NSError *error) {
        if (!error) {
            NSString *titleString = result;
            self.tableView.tableHeaderView = [self getHeaderView:titleString];
            [self loadData];
        }
    }];
    
}

-(void)addCustomerAction:(id)sender{
    AddCustomerViewController *addCustomerViewController = [[AddCustomerViewController alloc] initWithBlock:^{
        [self loadData];
    }];
    [self.navigationController pushViewController:addCustomerViewController animated:YES];
}

-(UIView*)getHeaderView:(NSString*)title{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 70)];
    UIImageView *backView = [UIImageView new];
    backView.backgroundColor = [UIColor whiteColor];
    [view addSubview:backView];
    backView.sd_layout.leftSpaceToView(view, 0).topSpaceToView(view, 0).widthIs(SCREEN_WIDTH).heightIs(60);

    UILabel *titleLabel = [UILabel new];
    titleLabel.text = [NSString stringWithFormat:@"用户总收益:%@元",title];
    [view addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(view, 15).topSpaceToView(view, 0).widthIs(SCREEN_WIDTH-15).heightIs(60);
    
    return view;
}

-(void)loadData{
    [self.tableView.mj_header beginRefreshing];
   
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.resultArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = 0;
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.tag = 10;
        titleLabel.text = @"XXX";
        titleLabel.font = [UIFont systemFontOfSize:18];
        [cell.contentView addSubview:titleLabel];
        titleLabel.sd_layout.leftSpaceToView(cell.contentView, 15).topSpaceToView(cell.contentView, 10).widthIs(SCREEN_WIDTH-30).heightIs(20);
        
        UILabel *phoneLabel = [UILabel new];
        phoneLabel.tag = 11;
        phoneLabel.text = @"电话:XXX";
        phoneLabel.textColor = [UIColor grayColor];
        phoneLabel.font = [UIFont systemFontOfSize:12];
        [cell.contentView addSubview:phoneLabel];
        phoneLabel.sd_layout.leftSpaceToView(cell.contentView, 15).topSpaceToView(titleLabel, 5).widthIs(SCREEN_WIDTH-30).heightIs(14);
        
        UILabel *cardNoLabel = [UILabel new];
        cardNoLabel.tag = 12;
        cardNoLabel.text = @"身份证号:XXX";
        cardNoLabel.textColor = [UIColor grayColor];
        cardNoLabel.font = [UIFont systemFontOfSize:12];
        [cell.contentView addSubview:cardNoLabel];
        cardNoLabel.sd_layout.leftSpaceToView(cell.contentView, 15).topSpaceToView(phoneLabel, 5).widthIs(SCREEN_WIDTH-30).heightIs(14);
        
        UIView *lineView = [UIView new];
        lineView.backgroundColor = DefaultCellLineColor;
        [cell.contentView addSubview:lineView];
        lineView.sd_layout.bottomSpaceToView(cell.contentView, 0).leftSpaceToView(cell.contentView, 0).widthIs(SCREEN_WIDTH).heightIs(1);
    }
    // Configure the cell...
    CustomerInfo *customerInfo = self.resultArray[indexPath.row];
    UILabel *titleLabel = [cell.contentView viewWithTag:10];
    titleLabel.text = customerInfo.realName;
    UILabel *phoneLabel = [cell.contentView viewWithTag:11];
    phoneLabel.text = [NSString stringWithFormat:@"电话:%@",customerInfo.phone];
    UILabel *cardNoLabel = [cell.contentView viewWithTag:12];
    cardNoLabel.text = [NSString stringWithFormat:@"身份证号:%@",customerInfo.idCard];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CustomerInfo *customerInfo = self.resultArray[indexPath.row];
    CustomerDetailViewController *customerDetailViewController = [[CustomerDetailViewController alloc] initWithCustomerInfo:customerInfo];
    [self.navigationController pushViewController:customerDetailViewController animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
